<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Info extends CI_Controller {



		function __construct()
	{
		parent::__construct();
		$this->load->library('ion_auth');
		$this->load->library('session');
		$this->load->library('form_validation');
		$this->load->database();
		$this->load->helper(array('url','language'));
    $this->lang->load('general');
    $this->data['user'] = $this->ion_auth->user()->row();
	}

  function team($team_id)
  {
    $this->load->model(array('teams_model', 'riders_model'));
    $this->data['team'] = $this->teams_model->get_by_id($team_id);
    $this->data['riders'] = $this->riders_model->get_by_team_number($this->data['team']->team_number);

    $team_total_points = 0;
    $old_max = 0;
    $this->data['top_rider'] = FALSE;
    foreach ($this->data['riders'] as $rider)
    {
      $rider_points = array_merge(explode(',', $rider->points_stage), explode(',', $rider->points_stage_yellow), explode(',', $rider->points_stage_green), explode(',', $rider->points_stage_mountain));
      $rider->total_points = array_sum($rider_points);
      $team_total_points = $team_total_points + array_sum($rider_points);
      if ($rider->total_points > $old_max)
      {
        $old_max = $rider->total_points;
        $this->data['top_rider'] = $rider;
      }
    }
    $this->data['team_total_points'] = $team_total_points;
    $this->data['title'] = $this->data['team']->team_name;
    $this->data['view'] = 'info/team';
    
    $this->load->view('template', $this->data);
  }

  function rider($rider_id)
  {
    $this->load->model(array('riders_model', 'stages_model', 'selections_model'));

    $this->data['rider'] = $this->riders_model->get_by_id($rider_id);

    foreach(explode(',',$this->data['rider']->points_stage) as $stage_id => $points)
    {
      $stages[$stage_id] = $this->stages_model->get_by_id($stage_id);
      $this->data['rider']->stage_points[$stage_id] = $points;
    }

    foreach(explode(',',$this->data['rider']->points_stage_yellow) as $stage_id => $points)
    {
      $stages[$stage_id] = $this->stages_model->get_by_id($stage_id);
      $this->data['rider']->yellow_points[$stage_id] = $points;
    }

    foreach(explode(',',$this->data['rider']->points_stage_green) as $stage_id => $points)
    {
      $stages[$stage_id] = $this->stages_model->get_by_id($stage_id);
      $this->data['rider']->green_points[$stage_id] = $points;
    }

    foreach(explode(',',$this->data['rider']->points_stage_mountain) as $stage_id => $points)
    {
      $stages[$stage_id] = $this->stages_model->get_by_id($stage_id);
      $this->data['rider']->mountain_points[$stage_id] = $points;
    }

    $this->data['selections'] = $this->selections_model->get_all();
    $this->data['number_of_selections'] = 0;
    foreach($this->data['selections'] as $sel)
    {
      if(in_array($rider_id, explode(',', $sel->selection)))
      {
        $this->data['user_teams'][] = $sel;
        $this->data['number_of_selections']++;
      }
    }

    $this->data['title'] = $this->data['rider']->last_name." ".$this->data['rider']->first_name;
    $this->data['view'] = 'info/rider';
    
    $this->load->view('template', $this->data);

  }

  function user_teams($user_id)
  {
    $this->load->model(array('riders_model', 'points_model', 'selections_model', 'users_model'));

    $this->data['selection'] = $this->selections_model->get_by_user_id($user_id);

    if(is_object($this->data['selection']))
    {
      $riders = explode(',', $this->data['selection']->selection);

      foreach ($riders as $rider_id)
      {
        $this->data['riders'][] = $this->riders_model->get_by_id($rider_id);
      }
    }
    $this->data['points'] = $this->points_model->get_by_user_id($user_id);
    $this->data['user_info'] = $this->users_model->get_by_id($user_id);
    $this->data['title'] = $this->data['user_info']->username;
    $this->data['view'] = 'info/user_teams';
    
    $this->load->view('template', $this->data);
  }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */